How can I combine my FTP queries? [migrated]

Posted by ryansworld10 on Programmers See other posts from Programmers or by ryansworld10
Published on 2012-12-06T18:04:51Z Indexed on 2012/12/07 5:21 UTC
Read the original article Hit count: 183

Filed under:
|

My program has several times where it queries an FTP server to read and upload information. How can I combine all these into one FTP class to handle everything?

    private static void UploadToFTP(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0]);
            request.Method = WebRequestMethods.Ftp.MakeDirectory;

            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        }
        catch
        {
        }

        try 
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + Path.GetFileName(file));
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            StreamReader source = new StreamReader(file);
            byte[] fileContents = Encoding.UTF8.GetBytes(source.ReadToEnd());
            source.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();

            RegenLog();
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}Upload Failed - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

    private static void CheckBlacklist(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + "blacklist.txt");
            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (TextReader reader = new StreamReader(stream))
                    {
                        string blacklist = reader.ReadToEnd();

                        if (blacklist.Contains(Environment.UserName))
                        {
                            File.AppendAllText(file, string.Format("{0}{0}Logger terminated - ({2}) - {1}{0}", nl, System.DateTime.Now, "Blacklisted"));
                            uninstall = true;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}FTP Error - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

    private static void CheckUpdate(string[] FTPSettings)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPSettings[0] + "update.txt");
            request.Credentials = new NetworkCredential(FTPSettings[1], FTPSettings[2]);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (TextReader reader = new StreamReader(stream))
                    {
                        string newVersion = reader.ReadToEnd();

                        if (newVersion != version)
                        {
                            update = true;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            File.AppendAllText(file, string.Format("{0}{0}FTP Error - ({2}) - {1}{0}", nl, System.DateTime.Now, e.Message.ToString()));
        }
    }

I know my code is also a bit inconsistent and messy, however this is my first time working with FTP in C#. Please give any advice you have!

© Programmers or respective owner

Related posts about c#

Related posts about ftp